import json

from django.http import HttpResponse
from django.shortcuts import render, redirect
from app01.models import UserInfo,Department

# urls.py里面函数存放在这个文件中
# Create your views here.
def index(request):
    return HttpResponse("欢迎使用")  # 返回一个普通字符串


def user_list(request):
    # 默认会去这个app下的templates目录下找模板 (根据app的注册顺序,逐一去他们的templates目录下找)
    return render(request, "app01_user_list.html")  # 返回一个html代码


def user_tpl(request):
    name = "hanchao"
    roles = ["男人", "儿子", "父亲", "ceo"]  #列表
    user_info = {  #字典
        'name': "guozhi",
        'sex': "nan",
        'age': 20
    }
    return render(request, "app01_user_tpl.html", {"n1": name, "roles": roles,'user_info':user_info})

def user_news(request):
    #python -m pip install requests
    import requests
    res = requests.get("http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/08/news?format=json")
    data_list=res.json() #这里我不行
    print(data_list)
    return render(request,"app01_user_news.html")

def user_something(req):
    print(req.method)
    print(req.GET) #<QueryDict: {'NAME': ['GGC']}> 这是get过来的url中的请求体
    #return HttpResponse("<h1>user_something</h1>")
    return redirect("https://www.baidu.com")

def user_login(req):
    if req.method=='GET':
        return render(req,"app01_user_login.html")
    else:
        print(req.POST)
        username=req.POST.get("username")
        password=req.POST.get("password")
        if username=="zhangsan" and password == "123456":
            #return redirect("https://www.baidu.com")
            return redirect(user_person)
        else:
            return HttpResponse("用户名或密码错误")


def user_orm(req):
    UserInfo.objects.create(name="ggc",password="1",age=10,size=175) #增加数据
    Department.objects.create(title="销售部")
    UserInfo.objects.filter(id=112).delete() #删除数据
    someUser=UserInfo.objects.filter(name="ggc") #查询
    allData=UserInfo.objects.all() #qureyset 类型 == 列表 类型
    for obj in allData: #遍历数据
        print(obj.name)
    for obj in someUser:
        print(obj.id)
    oneObject=UserInfo.objects.filter(name="ggc").first() #取第一个数据
    print(oneObject.name)
    UserInfo.objects.all().update(age=13) #更新
    return HttpResponse("添加成功")

def user_info_list(req):
    return HttpResponse('<h1>user_info_list</h1>')

def user_person(req):
    return HttpResponse("登陆成功")

def user_getjson(req):
    oneObject=UserInfo.objects.filter(name="ggc") #取第一个数据

    from django.core import serializers
    data = serializers.serialize("json", oneObject)
    #data1 = serializers.serialize("json", SomeModel.objects.filter(myfield1=myvalue))
    # # j=json.load(oneObject)
    print(data)
    return HttpResponse('')

def doSql(sql):
    import pymysql
    # 1.链接MySql
    conn = pymysql.connect(host="10.10.4.84",port=11055,user='root',password="QRG40oC6ZXuuDyY8",charset="utf-8",db="gx_day15")
    cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
    # 2.发送命令
    cursor.execute(sql)
    conn.commit()
    # 3.关闭
    cursor.close()
    conn.close()

def selectUserByName(name):
    sql="select * from app01_userinfo where name ='"+name+"'"
    result=doSql(sql)
    print(result)

def user_testpymysql(req):
    selectUserByName("ggc")
    return HttpResponse('')
